﻿using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Windows;
using System.Windows.Controls;
using WpfApp1.Enum;
using WpfApp1.Model;
using WpfApp1.ViewModel;

namespace WpfApp1.View
{
    /// <summary>
    /// TrainPage.xaml 的交互逻辑
    /// </summary>
    public partial class TrainPage : UserControl
    {
        static TrainViewModel trainViewModel = new TrainViewModel();

        public TrainPage()
        {
            InitializeComponent();
            DataContext = trainViewModel;
        }

        private void InitList()
        {

        }

        // 编辑更多信息
        private void EditPage(object sender, RoutedEventArgs e)
        {
            if (!(list.SelectedItem is TrainModel train))
            {
                return;
            }

            TrainDetail trainDetail = new TrainDetail
            {
                DataContext = train
            };
            trainDetail.InitForm();
            Train.trainTabs.Items.Add(new TabItem()
            {
                Header = "编辑培训",
                Content = trainDetail
            });
            Train.tabNum++;
            Train.trainTabs.SelectedIndex = Train.tabNum;
        }

        // 删除菜单
        private void DelTrain(object sender, RoutedEventArgs e)
        {
            if (!(list.SelectedItem is TrainModel trainModel))
            {
                return;
            }

            string sql_del = "delete from train where id=" + trainModel.Id;
            SQLiteCommand command = new SQLiteCommand(sql_del, MainWindow.connecttion);
            command.ExecuteNonQuery();
            trainViewModel.Trains.RemoveAt(list.SelectedIndex);
        }

        // 编辑列表
        private void List_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            TrainModel trainModel = list.SelectedItem as TrainModel;
            long id = trainModel.Id;
            long parent = trainModel.ParentId;
            TrainSubject subject = trainModel.Subject;
            List<ProductKind> kinds = trainModel.Kinds;
            TrainType type = trainModel.Type;
            string area = trainModel.Area;
            string shop = trainModel.ChainShop;
            DateTime start = trainModel.StartTime;
            DateTime end = trainModel.EndTime;

            string kindStr = "";
            foreach (ProductKind kindOne in kinds)
            {
                kindStr += "," + kindOne;
            }
            string allKindStr = kindStr.TrimStart(",".ToCharArray());
            trainModel.Kind = allKindStr;
            string sql;
            if (id == 0)
            {
                sql = "insert into train (parent_id,subject,kind,type,area,chain_shop,start_time,end_time) values (" + parent + "," + (int)subject + ",\"" + allKindStr + "\"," + (int)type + ",\"" + area + "\",\"" + shop + "\",\""
                    + start.ToString() + "\",\"" + end.ToString() + "\")";
            }
            else
            {
                sql = "update train set parent_id=" + parent + ",subject=" + (int)subject + ",kind=\"" + allKindStr + "\",type=" + (int)type + ",area=\"" + area + "\",chain_shop=\"" + shop + "\",start_time=\""
                    + start.ToString() + "\",end_time=\"" + end.ToString() + "\" where id=" + id;
            }

            SQLiteCommand command = new SQLiteCommand(sql, MainWindow.connecttion);
            command.ExecuteNonQuery();

            if (id == 0)
            {
                string sql_getid = "select last_insert_rowid() from train";
                command = new SQLiteCommand(sql_getid, MainWindow.connecttion);
                var newId = command.ExecuteScalar();
                trainModel.Id = Convert.ToInt64(newId);
            }
        }

        private void List_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
        {
            TrainModel train = e.Row.Item as TrainModel;
            if (train.Id == 0)
            {
                train.StartTime = DateTime.Now;
                train.EndTime = DateTime.Now;
            }
        }
    }
}
